﻿namespace FFF.Test.Application.DomainServices.CF
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Abp.Domain.Services;
    using Abp.Domain.Uow;
    using Abp.EntityFrameworkCore.Uow;
    using Abp.EntityFrameworkCore;
    using AutoMapper;
    using Abp.UI;
    using Ydm.Helper;
    using Ydm.Extensions;
    using Microsoft.EntityFrameworkCore;
    using AutoMapper.QueryableExtensions;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.Application.Repositories.CF;
    using FFF.Test.Entity.CF;
    using FFF.Test.Entity.CF.Dto;
    using FFF.Test.Entity.CF.View;

    /// <summary>
    /// 配置类业务参数表领域层服务接口
    /// </summary>
    public class CFBizConfigOptionDomainService : YdmAbpDomainServiceBase, ICFBizConfigOptionDomainService
    {
        /// <summary>
        /// 配置类业务参数表仓储
        /// </summary>
        private readonly ICFBizConfigOptionRepository _cFBizConfigOptionRepository;

        private readonly ICFBizConfigGroupRepository _cFBizConfigGroupRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="CFBizConfigOptionDomainService"/> class.
        /// 配置类业务参数表领域层操作
        /// </summary>
        /// <param name="cFBizConfigOptionRepository">配置类业务参数表仓储</param>
        /// <param name="cFBizConfigGroupRepository">cf仓储</param>
        public CFBizConfigOptionDomainService(
            ICFBizConfigOptionRepository cFBizConfigOptionRepository,
            ICFBizConfigGroupRepository cFBizConfigGroupRepository
        )
        {
            this._cFBizConfigOptionRepository = cFBizConfigOptionRepository;
            this._cFBizConfigGroupRepository = cFBizConfigGroupRepository;
        }

        /// <summary>
        /// 引入业务参数(于大明)
        /// </summary>
        /// <param name="inDto">引入业务参数输入dto</param>
        /// <returns>引入业务参数输出dto</returns>
        public async Task<ImportConfigOptionOutDto> ImportConfigOptionAsync(ImportConfigOptionInDto inDto)
        {
            var result = new ImportConfigOptionOutDto();
            Guid? scopGUID = null;
            if (inDto.FromScopeGUID != Guid.Empty)
            {
                scopGUID = inDto.FromScopeGUID;
            }

            var viewGroup = this._cFBizConfigGroupRepository.GetView();
            var viewOption = this._cFBizConfigOptionRepository.GetView();
            var groupDtos = await viewGroup.Where(x => x.BizGroupScopeGUID == scopGUID).ProjectTo<CFBizConfigGroupCreateInputDto>().ToListAsync();
            var optionsDtos = await viewOption.Where(x => x.BizOptionScopeGUID == scopGUID).ProjectTo<CFBizConfigOptionCreateInputDto>().ToListAsync();

            foreach (var groupDto in groupDtos)
            {
                var groupId = Guid.NewGuid();
                foreach (var optDto in optionsDtos.Where(x => x.BizConfigGroupGUID == groupDto.Id))
                {
                    optDto.Id = Guid.NewGuid();
                    optDto.BizConfigGroupGUID = groupId;
                    optDto.BizOptionScopeGUID = inDto.TargetScopeGUID;
                    await this._cFBizConfigOptionRepository.CreateByDtoAsync(optDto);
                }

                groupDto.Id = groupId;
                groupDto.BizGroupScopeGUID = inDto.TargetScopeGUID;
                await this._cFBizConfigGroupRepository.CreateByDtoAsync(groupDto);

            }

            return result;
        }

        /// <summary>
        /// 批量保存业务参数(于大明)
        /// </summary>
        /// <param name="inDto">批量保存业务参数输入dto</param>
        /// <returns>批量保存业务参数输出dto</returns>
        public async Task<BatchSaveOptionsOutDto> BatchSaveOptionsAsync(BatchSaveOptionsInDto inDto)
        {
            List<Guid> ids = new List<Guid>();
            List<string> values = new List<string>();

            var result = new BatchSaveOptionsOutDto();
            for (int i = 0; i < inDto.Ids.Count; i++)
            {
                var index = ids.FindLastIndex(x => x == inDto.Ids[i]);
                if (index == -1)
                {
                    ids.Add(inDto.Ids[i]);
                    values.Add(inDto.Values[i]);
                }
                else
                {
                    values[index] = values[index] + "," + inDto.Values[i];
                }
            }

            for (int i = 0; i < ids.Count; i++)
            {
                CFBizConfigOptionUpdateInputDto updateInputDto = new CFBizConfigOptionUpdateInputDto()
                {
                    Id = ids[i],
                    BizOptionScopeGUID = inDto.BizOptionScopeGUID,
                    BizOptionValue = values[i]
                };

                await this._cFBizConfigOptionRepository.UpdateByDtoAsync(updateInputDto);
            }

            return result;
        }
    }
}
